package com.service.thirddatacenter.hpool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.thirddatacenter.chiaexplorer.client.ChiaExplorerClient;
import com.service.thirddatacenter.chiaexplorer.model.entity.BlockChainRecordEntity;
import com.service.thirddatacenter.chiaexplorer.service.BlockChainRecordService;
import com.service.thirddatacenter.enums.PlatformEnum;
import com.service.thirddatacenter.hpool.client.HPoolClient;
import com.service.thirddatacenter.hpool.enums.MinerStatus;
import com.service.thirddatacenter.hpool.model.entity.AccountThirdEntity;
import com.service.thirddatacenter.hpool.rep.AccountHashRateRep;
import com.service.thirddatacenter.hpool.service.AccountMinerService;
import com.service.thirddatacenter.hpool.service.AccountShareService;
import com.service.thirddatacenter.hpool.service.AccountThirdService;
import com.service.thirddatacenter.hpool.service.MiningRecordService;
import com.service.utility.common.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("hPool/data")
public class HPoolDataController extends BaseController {

	@Autowired
	private ChiaExplorerClient chiaExplorerClient;

	@Autowired
	private BlockChainRecordService blockChainRecordService;

	@Autowired
	private AccountMinerService accountMinerService;

	@Autowired
	private AccountThirdService accountThirdService;

	@Autowired
	private AccountShareService accountShareService;

	@Autowired
	private MiningRecordService miningRecordService;

	@Autowired
	private HPoolClient hPoolClient;

	String type = "chia";


	/**
	 * @Description: 获取账户全网算力和每T产出
	 * @Author: ty
	 * @Date: 2021/6/22 14:34
	 * @param: []
	 * @return: com.service.core.rest.response.BaseResponse
	 **/
	@GetMapping("account-hash-rate")
	public AccountHashRateRep account() {
		AccountHashRateRep rep = new AccountHashRateRep();
		rep.setMiningRate(BigDecimal.ZERO);
		rep.setTotalNetworkHashRate(BigDecimal.ZERO);
		//获取商户所有有效矿机
		List<AccountThirdEntity> accounts = accountThirdService.listAllAccount();
		if (ObjectUtils.isEmpty(accounts)) {
			return rep;
		}
		List<Integer> accountIds = accounts.stream().map(AccountThirdEntity::getAccountId).collect(Collectors.toList());
		//统计全网在线矿机算力
		BigDecimal totalOnlineNum = accountMinerService.totalOnline(accountIds, type, MinerStatus.online.getCode());
		if (totalOnlineNum.compareTo(BigDecimal.ZERO) == 0) {
			return rep;
		}
		rep.setTotalNetworkHashRate(totalOnlineNum);
		//统计今日收益产出总量
		BigDecimal totalRewardNum = BigDecimal.ZERO;
		for(Integer accountId:accountIds){
			BigDecimal rewardNum = miningRecordService.getLastIncomeByAccountId(accountId, type);
			totalRewardNum = totalRewardNum.add(rewardNum);
		}
		if (totalRewardNum.compareTo(BigDecimal.ZERO) == 0) {
			return rep;
		}
		//换算单位MiB转换为TiB
		BigDecimal totalTiB = MathUtil.div(totalOnlineNum, new BigDecimal(1024L),8);
		BigDecimal miningRate = MathUtil.div(totalRewardNum, totalTiB,8);
		rep.setMiningRate(miningRate);
		return rep;
	}

	/**
	 * @Description: 获取hpool全网信息
	 * @Author: ty
	 * @Date: 2021/6/22 14:34
	 * @param: []
	 * @return: com.service.core.rest.response.BaseResponse
	 **/
	@GetMapping("network-hash-rate")
	public OneResponse<BlockChainRecordEntity> networkData() {
		BlockChainRecordEntity entity = blockChainRecordService.getOne(new QueryWrapper<BlockChainRecordEntity>().lambda()
				.eq(BlockChainRecordEntity::getType, type)
		);
		return successQueryOneResponse(entity);
	}

}
